// ============================================================================
//
// Copyright (C) 2006-2007 Dengues
//
// Google Group: http://groups.google.com/group/dengues
// QQ Group: 24885404
//
// This library is free software; you can redistribute it and/or
// modify it under the terms of the GNU Lesser General Public
// License as published by the Free Software Foundation; either
// version 2.1 of the License.
//
// This library is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
// Lesser General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program; if not, write to the Free Software
// Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
//
// ============================================================================
package org.dengues.core.process;

import java.beans.PropertyChangeListener;
import java.beans.PropertyChangeSupport;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

import org.dengues.core.components.NodePropertyParameter;
import org.dengues.core.external.IExternalData;
import org.dengues.core.i18n.Messages;
import org.dengues.core.metadata.MetadataTable;
import org.eclipse.draw2d.geometry.Dimension;
import org.eclipse.draw2d.geometry.Point;
import org.eclipse.swt.graphics.RGB;

/**
 * Qiang.Zhang.Adolf@gmail.com class global comment. Detailled comment <br/>
 * 
 * $Id: Dengues.epf 2008-1-7 qiang.zhang $
 * 
 */
public abstract class CompElement implements Cloneable, Serializable, IGefNode {

    private List<String> dataUniques = new ArrayList<String>();

    private IExternalData externalData;

    public static final String PROP_INCOMINGS = "incomingConnections"; //$NON-NLS-1$

    public static final String PROP_OUTGOINGS = "outgoingConnections"; //$NON-NLS-1$

    public static final String ITEM_KEY = Messages.getString("MetadataTableViewer.key"); //$NON-NLS-1$

    private List<ICompConnection> incomingConnections;

    private List<ICompConnection> outgoingConnections;

    private ICompBlock parent;

    /**
     * Qiang.Zhang.Adolf@gmail.com CompElement constructor comment.
     */
    public CompElement() {
        incomingConnections = new ArrayList<ICompConnection>();
        outgoingConnections = new ArrayList<ICompConnection>();
    }

    /**
     * Qiang.Zhang.Adolf@gmail.com Comment method "getLocation".
     * 
     * @return
     */
    public Point getLocation() {
        return new Point(0, 0);
    }

    /**
     * Qiang.Zhang.Adolf@gmail.com Comment method "getSize".
     * 
     * @return
     */
    public Dimension getSize() {
        return new Dimension(0, 0);
    }

    /**
     * Qiang.Zhang.Adolf@gmail.com Comment method "getLocation".
     * 
     * @return
     */
    public void setLocation(Point loc) {

    }

    /**
     * Qiang.Zhang.Adolf@gmail.com Comment method "getSize".
     * 
     * @return
     */
    public void setSize(Dimension size) {
    }

    private List<NodePropertyParameter> elementParameters = new ArrayList<NodePropertyParameter>();

    private List<MetadataTable> metadataList = new ArrayList<MetadataTable>();

    // property change listeners
    protected PropertyChangeSupport listeners = new PropertyChangeSupport(this);

    /**
     * Qiang.Zhang.Adolf@gmail.com Comment method "addPropertyChangeListener".
     * 
     * @param listener
     */
    public void addPropertyChangeListener(PropertyChangeListener listener) {
        listeners.addPropertyChangeListener(listener);
    }

    /**
     * Qiang.Zhang.Adolf@gmail.com Comment method "firePropertyChange".
     * 
     * @param prop
     * @param old
     * @param newValue
     */
    protected void firePropertyChange(String prop, Object old, Object newValue) {
        listeners.firePropertyChange(prop, old, newValue);
    }

    /**
     * Qiang.Zhang.Adolf@gmail.com Comment method "fireStructureChange".
     * 
     * @param prop
     * @param child
     */
    protected void fireStructureChange(String prop, Object child) {
        listeners.firePropertyChange(prop, null, child);
    }

    // implemented in order to create listeners field
    /**
     * Qiang.Zhang.Adolf@gmail.com Comment method "readObject".
     * 
     * @param in
     * @throws IOException
     * @throws ClassNotFoundException
     */
    private void readObject(ObjectInputStream in) throws IOException, ClassNotFoundException {
        in.defaultReadObject();
        listeners = new PropertyChangeSupport(this);
    }

    /**
     * Qiang.Zhang.Adolf@gmail.com Comment method "removePropertyChangeListener".
     * 
     * @param l
     */
    public void removePropertyChangeListener(PropertyChangeListener l) {
        listeners.removePropertyChangeListener(l);
    }

    /**
     * Qiang.Zhang.Adolf@gmail.com Comment method "getCompName".
     * 
     * @return
     */
    public abstract String getCompName();

    /**
     * Qiang.Zhang.Adolf@gmail.com Comment method "getCompDisplayName".
     * 
     * @return
     */
    public String getCompDisplayName() {
        return getCompName();
    }

    /**
     * Qiang.Zhang.Adolf@gmail.com Comment method "getElementParameters".
     * 
     * @return
     */
    public List<NodePropertyParameter> getElementParameters() {
        return elementParameters;
    }

    /**
     * Qiang.Zhang.Adolf@gmail.com Comment method "setElementParameters".
     * 
     * @param elementParameters
     */
    public void setElementParameters(List<NodePropertyParameter> elementParameters) {
        this.elementParameters = elementParameters;
    }

    /**
     * Qiang.Zhang.Adolf@gmail.com Comment method "setElementParameters".
     * 
     * @param elementParameters
     */
    public void addElementParameters(NodePropertyParameter elementParameters) {
        this.elementParameters.add(elementParameters);
    }

    /**
     * Qiang.Zhang.Adolf@gmail.com Comment method "getElementParameter".
     * 
     * @param name
     * @return
     */
    public NodePropertyParameter getElementParameter(String name) {
        for (NodePropertyParameter parameter : elementParameters) {
            if (name.equals(parameter.getName())) {
                return parameter;
            }
        }
        return null;
    }

    /**
     * Qiang.Zhang.Adolf@gmail.com Comment method "setNodeParameterValue".
     * 
     * @param name
     * @param value
     */
    public void setNodeParameterValue(String name, Object value) {
        NodePropertyParameter elementParameter = getElementParameter(name);
        if (elementParameter != null) {
            elementParameter.setValue(value);
        }
    }

    /**
     * Getter for incomingConnections.
     * 
     * @return the incomingConnections
     */
    public List<ICompConnection> getIncomingConnections() {
        return this.incomingConnections;
    }

    /**
     * Getter for metadataList.
     * 
     * @return the metadataList
     */
    public List<MetadataTable> getMetadataList() {
        return this.metadataList;
    }

    /**
     * Sets the metadataList.
     * 
     * @param metadataList the metadataList to set
     */
    public void setMetadataList(List<MetadataTable> metadataList) {
        this.metadataList = metadataList;
    }

    public abstract ICompProcess getCompProcess();

    /*
     * (non-Javadoc)
     * 
     * @see java.lang.Object#toString()
     */
    @Override
    public String toString() {
        String s = getCompName();
        s = s + ":"; //$NON-NLS-1$
        return s;
    }

    /*
     * (non-Javadoc)
     * 
     * @see java.lang.Object#clone()
     */
    @Override
    public Object clone() {
        Object object = null;
        try {
            object = super.clone();

        } catch (Exception e) {
            e.printStackTrace();
        }
        return object;
    }

    /**
     * Qiang.Zhang.Adolf@gmail.com Comment method "getColor".
     * 
     * @return
     */
    public RGB getColor() {
        return new RGB(0, 0, 0);
    }

    /**
     * Sets the incomingConnections.
     * 
     * @param incomingConnections the incomingConnections to set
     */
    public void addIncomingConnections(ICompConnection incomingConnection) {
        incomingConnections.add(incomingConnection);
        fireStructureChange(PROP_INCOMINGS, incomingConnections);
    }

    public void removeIncomingConnections(ICompConnection incomingConnection) {
        incomingConnections.remove(incomingConnection);
        fireStructureChange(PROP_INCOMINGS, incomingConnections);
    }

    /**
     * Getter for outgoingConnections.
     * 
     * @return the outgoingConnections
     */
    public List<ICompConnection> getOutgoingConnections() {
        return this.outgoingConnections;
    }

    /**
     * Sets the outgoingConnections.
     * 
     * @param outgoingConnections the outgoingConnections to set
     */
    public void addOutgoingConnections(ICompConnection outgoingConnection) {
        this.outgoingConnections.add(outgoingConnection);
        fireStructureChange(PROP_OUTGOINGS, outgoingConnections);
    }

    /**
     * Qiang.Zhang.Adolf@gmail.com Comment method "removeOutgoingConnections".
     * 
     * @param outgoingConnection
     */
    public void removeOutgoingConnections(ICompConnection outgoingConnection) {
        this.outgoingConnections.remove(outgoingConnection);
        fireStructureChange(PROP_OUTGOINGS, outgoingConnections);
    }

    /**
     * Sets the incomingConnections.
     * 
     * @param incomingConnections the incomingConnections to set
     */
    public void setIncomingConnections(List<ICompConnection> incomingConnections) {
        this.incomingConnections = incomingConnections;
    }

    /**
     * Sets the outgoingConnections.
     * 
     * @param outgoingConnections the outgoingConnections to set
     */
    public void setOutgoingConnections(List<ICompConnection> outgoingConnections) {
        this.outgoingConnections = outgoingConnections;
    }

    /*
     * (non-Javadoc)
     * 
     * @see org.dengues.core.process.IGefNode#getUniqueName()
     */
    public String getUniqueName() {
        return getCompName();
    }

    /*
     * (non-Javadoc)
     * 
     * @see org.dengues.core.process.IGefNode#isInput(java.lang.String)
     */
    public boolean isInput(String string) {
        return false;
    }

    /*
     * (non-Javadoc)
     * 
     * @see org.dengues.core.process.IGefNode#isOutput(java.lang.String)
     */
    public boolean isOutput(String string) {
        return false;
    }

    /**
     * Getter for parent.
     * 
     * @return the parent
     */
    public ICompBlock getParent() {
        return this.parent;
    }

    /**
     * Sets the parent.
     * 
     * @param parent the parent to set
     */
    public void setParent(ICompBlock parent) {
        this.parent = parent;
    }

    /**
     * Getter for externalData.
     * 
     * @return the externalData
     */
    public IExternalData getExternalData() {
        return this.externalData;
    }

    /**
     * Sets the externalData.
     * 
     * @param externalData the externalData to set
     */
    public void setExternalData(IExternalData externalData) {
        this.externalData = externalData;
    }

    /**
     * Getter for dataUniques.
     * 
     * @return the dataUniques
     */
    public List<String> getDataUniques() {
        return this.dataUniques;
    }

    /**
     * Sets the dataUniques.
     * 
     * @param dataUniques the dataUniques to set
     */
    public void setDataUniques(List<String> dataUniques) {
        this.dataUniques = dataUniques;
    }

    /*
     * (non-Javadoc)
     * 
     * @see org.dengues.core.process.IGefNode#isSubProcessStart()
     */
    public boolean isSubProcessStart() {
        return false;
    }

    /*
     * (non-Javadoc)
     * 
     * @see org.dengues.core.process.IGefNode#isStart()
     */
    public boolean isStart() {
        return false;
    }

}
